package daosimpl;

import java.util.ArrayList;
import java.util.List;

import model.CursoPeridoGrupo;
import model.Matricula;
import model.Profesor;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;

import util.HibernateUtil;
import daos.MatriculaDAO;

public class MatriculaDAOImplH implements MatriculaDAO {
	private Transaction tx;
	
	@Override
	public List<Matricula> ListarMatriculados() throws HibernateException {
		List<Matricula> result = new ArrayList<Matricula>();
		try {			
			tx = HibernateUtil.session.beginTransaction();						
			Query query = HibernateUtil.session
					.createQuery("FROM matricula");
			result = query.list();
			System.out.println(query.list());
			System.out.println(query.list().size());
			if (query.list().isEmpty()) {
				System.out.println("esta vacio");
			}
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}			
			return result;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return result; 
	}

	@Override
	public List<Matricula> ListarMatriculadosxCursoPeriodoGrupo(
			CursoPeridoGrupo cpg) throws HibernateException {
		List<Matricula> result = new ArrayList<Matricula>();
		try {			
			tx = HibernateUtil.session.beginTransaction();						
			Query query = HibernateUtil.session
					.createQuery("FROM Matricula where curso_perido_grupo_id='"+cpg.getCursoPeridoGrupoId()+"'");
			result = query.list();
			System.out.println(query.list());
			System.out.println(query.list().size());
			if (query.list().isEmpty()) {
				System.out.println("esta vacio");
			}
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}			
			return result;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return result; 
	}

	@Override
	public List<?> ListarMatriculadosxProfesor(Profesor prof)
			throws HibernateException {
		List<?> result = new ArrayList<Object>();
		try {			
			tx = HibernateUtil.session.beginTransaction();						
			Query query = HibernateUtil.session
					.createQuery("SELECT * FROM matricula as mat,curso_perido_grupo as cpg WHERE "
							+ "mat.curso_perido_grupo_id=cpg.curso_perido_grupo_id AND "
							+ "cpg.docente_id='"+prof.getDocenteId()+"'");
			result = query.list();
			System.out.println(query.list());
			System.out.println(query.list().size());
			if (query.list().isEmpty()) {
				System.out.println("esta vacio");
			}
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}			
			return result;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return result; 
	}

}
